//
//  EditMeController.swift
//  LiteAccount
//
//  Created by 刘伟 on 2017/5/27.
//  Copyright © 2017年 刘伟. All rights reserved.
//

import UIKit

class EditMeController: UIViewController {
    
    var nickname = "" {
        didSet {
            nicknameTextField.text = nickname
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        layoutUI()
        // Do any additional setup after loading the view.
    }
    
    fileprivate func prepareUI() {
        self.edgesForExtendedLayout = .init(rawValue: 0)
        self.view.backgroundColor = UIColor.la_bg
        self.view.addSubview(nicknameTextField)
        self.view.addSubview(submitBtn)
    }
    
    fileprivate func layoutUI() {
        nicknameTextField.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(50)
        }
        
        submitBtn.snp.makeConstraints { (make) in
            make.left.equalTo(10)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-10)
            make.height.equalTo(44)
        }
    }
    
    func submit() {
        debugPrint("提交更新用户信息")
        var params = [String: Any]()
        let currentNickname = nicknameTextField.text!
        if currentNickname == "" {
            ProgressHUD.showError("昵称不能为空")
            return
        }
        params["token"] = Defaults[.token]
        params["nickname"] = currentNickname
        APIClient.shared.requestUpdateNickname(params: params) { (response) in
            switch response {
            case .success(_,_):
                self.navigationController?.popViewController(animated: true)
            default: break
            }
        }
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    fileprivate lazy var nicknameTextField: UITextField = {
        let object = UITextField()
        object.borderStyle = .none
        object.placeholder = "请输入昵称"
        object.font = UIFont.la_Font(ofSize: 15)
        object.backgroundColor = .white
        // 为了间隔
        object.leftView = UIView(frame:CGRect(x: 0, y: 0, width: 10, height: 44))
        object.leftViewMode = UITextFieldViewMode.always
        object.returnKeyType = .next
        return object
    }()
    
    // 发送邮件按钮
    lazy var submitBtn: UIButton = {
        let object = UIButton()
        object.layer.cornerRadius = 2
        object.backgroundColor = UIColor.primary
        object.setTitle("更新用户信息", for: .normal)
        object.setTitleColor(UIColor.white, for: .normal)
        object.addTarget(self, action: #selector(submit), for:
            .touchUpInside)
        return object
    }()

}
